<template>
  <div class="container">
    <div class="query-container">
      <el-card class="query-card">
        <el-form
          :model="queryForm"
          class="query-form"
          ref="formRef"
          label-width="120px"
        >
          <el-row>
            <el-col :span="6">
              <el-form-item label="专利名称">
                <el-input
                  v-model="queryForm.patentName"
                  placeholder="请输入专利名称"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="专利类别">
                <el-select v-model="queryForm.patentCategoryId" placeholder="请选择专利类别">
                  <el-option
                    v-for="item in patentCategories"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-button type="primary" @click="handleSearch()">查询</el-button>
              <el-button @click="reset()">重置</el-button>
              <el-button type="warning" @click="handleAdd()">新增</el-button>
              <el-button type="danger" @click="batchDelete()">删除</el-button>
            </el-col>
          </el-row>

        </el-form>
      </el-card>
    </div>
    <div class="table-container">
      <el-table
        :data="tableData"
        border
        fit
        @selection-change="handleSelectionChange"
        size="mini"
        style="width: 100%"
      >
        <el-table-column fixed type="selection" width="55" />
        <el-table-column
          fixed
          prop="id"
          label="ID"
          width="80"
        ></el-table-column>
        <el-table-column
          fixed
          prop="patentName"
          label="专利名称"
          width="200"
        ></el-table-column>
        <el-table-column
          fixed
          prop="patentCode"
          label="专利编号"
          width="150"
        ></el-table-column>
        <el-table-column prop="patentCategoryName" label="专利类别" width="150">
          <template slot-scope="scope">
            {{ scope.row.patentCategoryName }}
          </template>
        </el-table-column>
        <el-table-column
          prop="company"
          label="公司名称"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="inventor"
          label="发明人"
          width="120"
        ></el-table-column>
        <el-table-column
          prop="patentOwner"
          label="专利所有人"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="applicationNum"
          label="申请号"
          width="150"
        ></el-table-column>
        <el-table-column prop="applicationDate" label="申请日期" width="150">
          <template slot-scope="scope">
            <el-date-picker
              v-model="scope.row.applicationDate"
              type="date"
              disabled
            />
          </template>
        </el-table-column>
        <el-table-column prop="registerDate" label="登记日期" width="150">
          <template slot-scope="scope">
            <el-date-picker
              v-model="scope.row.registerDate"
              type="date"
              disabled
            />
          </template>
        </el-table-column>
        <el-table-column
          prop="patentNum"
          label="专利号"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="patentTerm"
          label="专利期限"
          width="120"
        ></el-table-column>
        <el-table-column
          prop="authorizationDate"
          label="专利授权日期"
          width="150"
        >
          <template slot-scope="scope">
            {{ scope.row.authorizationDate }}
          </template>
        </el-table-column>
        <el-table-column prop="getMethod" label="获取方式" width="120">
          <template slot-scope="scope">
            {{ scope.row.getMethod === 1 ? "方法一" : "方法二" }}
            <!-- 根据实际需要定义获取方式 -->
          </template>
        </el-table-column>
        <el-table-column
          prop="remark"
          label="备注"
          width="200"
        ></el-table-column>
        <el-table-column
          prop="creatorName"
          label="创建人"
          width="120"
        ></el-table-column>
        <el-table-column prop="created" label="创建时间" width="200">
          <template slot-scope="scope">
            <el-date-picker
              v-model="scope.row.created"
              type="datetime"
              disabled
            />
          </template>
        </el-table-column>
        <el-table-column prop="updated" label="更新时间" width="200">
          <template slot-scope="scope">
            <el-date-picker
              v-model="scope.row.updated"
              type="datetime"
              disabled
            />
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              @click="handleDetail(scope.row.id)"
              type="text"
              size="small"
              >查看</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="handleEdit(scope.row.id)"
              >编辑</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="handleDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[1, 10, 20, 50, 100]"
        :page-size="limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="count"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { patent } from "@/api/patent";
import { patentCategory } from "@/api/patentCategory";
import userService from "@/api/user";
export default {
  data() {
    return {
      tableData: [],
      page: 1,
      limit: 10,
      count: 0,
      queryForm: {
        patentName: "",
        patentCategoryId: ""
      },
      patentCategories: [],
      creators: [],
    };
  },
  created() {
    this.getPatentList();
    this.getPatentCategories();
    this.getCreators();
  },
  methods: {
    getPatentList() {
      patent.list({ page: this.page, limit: this.limit, ...this.queryForm }).then((response) => {
        this.tableData = response.data;
        this.count = response.count;
      });
    },
    getPatentCategories() {
      patentCategory.findAll().then((response) => {
        this.patentCategories = response.data;
      });
    },
    getCreators() {
      userService.findAll().then((response) => {
        this.creators = response.data;
      });
    },
    handleSearch() {
      this.getPatentList();
    },
    reset() {
      this.queryForm = {};
      this.getPatentList();
    },
    handleAdd() {
      // 跳转新增页面
      this.$router.push({
        name: "PatentAdd",
      });
    },
    batchDelete() {
      const ids = this.multipleSelection.map((item) => item.id);
      if (ids.length === 0) {
        this.$message({
          message: "请选择要删除的数据",
          type: "warning",
        });
        return;
      }
      const batchDeleteDto = {
        ids: ids,
      };
      // console.log(batchDeleteDto);
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 删除
          patent.deleteBatch(batchDeleteDto).then((response) => {
            this.$message({
              message: response.msg,
            });
            this.getPatentList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log(this.multipleSelection);
    },
    handleDetail(id) {
      // 跳转详情页面
      this.$router.push({
        name: "PatentDetail",
        params: { id: id },
      });
    },
    handleEdit(id) {
      // 跳转编辑页面
      this.$router.push({
        name: "PatentEdit",
        params: { id: id },
      });
    },
    handleDelete(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 删除
          patent.delete(id).then((response) => {
            this.$message({
              message: response.msg,
            });
            this.getPatentList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.limit = val;
      this.getPatentList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      this.getPatentList();
    },
  },
};
</script>

<style scoped>
.container {
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.query-container {
  margin-bottom: 20px;
}

.query-card {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* .button-group {
  display: flex;
  justify-content: space-between;
} */

.table-container {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  background-color: white;
}

.pagination-container {
  margin-top: 20px;
}
</style>
